package com.hctforgreen.greenservice.ui.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.HandlerThread;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hctforgreen.greenservice.MessageDetailActivity;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.StudyDetailActivity;
import com.hctforgreen.greenservice.TerminalStudyDetailActivity;
import com.hctforgreen.greenservice.db.DBMessageManager;
import com.hctforgreen.greenservice.model.BaseEntity;
import com.hctforgreen.greenservice.model.BookListEntity.BookEntity;
import com.hctforgreen.greenservice.model.MessagesListEntity.MessagesEntity;
import com.hctforgreen.greenservice.model.StudyEntity;
import com.hctforgreen.greenservice.ui.handler.RecentMessageListHandler;
import com.hctforgreen.greenservice.ui.handler.RecentTerminalDataListHandler;
import com.hctforgreen.greenservice.ui.page.AbstractDataLoaderHandler;
import com.hctforgreen.greenservice.ui.page.AbstractPageableAdapter;
import com.hctforgreen.greenservice.utils.DownloadBookLstInitFinishUtil;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.Utils;
/**
 * 终端培训：最新学习资料
 * @author 390694
 *2016-9-26
 */
public class RecentTerminalDataListAdapter extends
		AbstractPageableAdapter<BaseEntity> implements OnItemClickListener {

	public static final String FRESH_MSG_DATA = "com.hctforgreen.greenservice.fresh_msg_data";

	private Activity mActivity;
	private ListView mListView;
	private RecentTerminalDataListHandler mHandler;


	public RecentTerminalDataListAdapter(ListView listView, Activity activity,
			int loadingViewResourceId,int reloadViewResourceId,
			AbstractDataLoaderHandler<BaseEntity> handler) {
		super(listView, activity, loadingViewResourceId, reloadViewResourceId,
				handler);
		mListView = listView;
		mActivity = activity;

		mHandler = (RecentTerminalDataListHandler) handler;
		mListView.setAdapter(this);
		mListView.setOnItemClickListener(this);
		mListView.setOnScrollListener(this);
		mListView.setDivider(mActivity.getResources().getDrawable(
				R.drawable.ic_driver));
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewCache viewCache;
		final StudyEntity entity = (StudyEntity) getItem(position);
		if (convertView == null) {
			convertView = View.inflate(mActivity,
					R.layout.item_recent_learn_data_lst, null);
			viewCache = new ViewCache(convertView);
			convertView.setTag(viewCache);
		} else {
			viewCache = (ViewCache) convertView.getTag();
		}
		viewCache.getDateTv().setText(entity.date);
		viewCache.getMsgTitleTv().setText(entity.title);
		viewCache.getReadedStatusCb().setVisibility(View.VISIBLE);		
		viewCache.getReadedStatusCb().setChecked(true);
		//20161114 390697 add
		if (!TextUtils.isEmpty(entity.examId)){
			//学习资料包含考试
			viewCache.getExamIcon().setVisibility(View.VISIBLE);
		}else{
			viewCache.getExamIcon().setVisibility(View.GONE);
		}
		return convertView;
	}


	class ViewCache {
		private View baseView;
		private TextView date;
		private TextView msgTitle;
		private CheckBox readedStatusCb;
		private ImageView examIcon;

		public ViewCache(View view) {
			baseView = view;
		}

		public TextView getDateTv() {
			if (date == null) {
				date = (TextView) baseView.findViewById(R.id.tv_date);
			}
			return date;
		}

		public TextView getMsgTitleTv() {
			if (msgTitle == null) {
				msgTitle = (TextView) baseView.findViewById(R.id.tv_msg_title);
			}
			return msgTitle;
		}

		public CheckBox getReadedStatusCb() {
			if (readedStatusCb == null) {
				readedStatusCb = (CheckBox) baseView
						.findViewById(R.id.cb_message_readed_status);
			}
			return readedStatusCb;
		}

		public ImageView getExamIcon(){
			if (examIcon == null){
				examIcon = (ImageView) baseView.findViewById(R.id.iv_hasEaxm_icon);
			}
			return examIcon;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			final int position, long id) {

		/**
		 * 2016-12-31 390694 增加判断更新资料的服务是否在进行，是  则提示
		 */
		boolean isServiceRunning = DownloadBookLstInitFinishUtil.getUpdateBookServiceFinished(mActivity);
		if (isServiceRunning){
			Dialog dialog = null;
			AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
			builder.setTitle(R.string.dialog_default_title_hint)
					.setMessage(R.string.update_now)
					.setPositiveButton(R.string.dialog_confirm_hint,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
													int which) {
									dialog.dismiss();
								}
							});
			dialog = builder.create();
			dialog.show();
			return;
		}

		final StudyEntity entity = (StudyEntity) getItem(position);
		Intent intent = new Intent();
		intent.setClass(mActivity, TerminalStudyDetailActivity.class);
		intent.putExtra(
				HctConstants.ON_ACTIVITY_KEY_STUDY_ENTITY,
				entity);
		mActivity.startActivity(intent);
	}

}
